<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('location_distance'))
{
	function location_distance($loc1, $loc2)
	{
		return geo_distance($loc1->latitude, $loc1->longitude,$loc2->latitude, $loc2->longitude, false);
	}
}


if ( ! function_exists('geo_distance'))
{
	function geo_distance($lat1, $lng1, $lat2, $lng2, $miles = false)
	{
		$pi80 = M_PI / 180;
		$lat1 *= $pi80;
		$lng1 *= $pi80;
		$lat2 *= $pi80;
		$lng2 *= $pi80;

		$r = 6372.797; // mean radius of Earth in km

		$dlat = $lat2 - $lat1;
		$dlng = $lng2 - $lng1;
		$a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
		$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
		$km = $r * $c;
		return ($miles ? ($km * 0.621371192) : $km);
	}
}


